<!-- templates/edit_item.html -->
<!DOCTYPE html>
<html>
<head>
    <title>级别编辑</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h1>级别编辑</h1>
        <div class="mb-3">
            <label for="level" class="form-label">级别</label>
            <input type="text" class="form-control" id="level" value="{{ title }}">
        </div>
        <div class="mb-3">
            <label for="discount" class="form-label">折扣</label>
            <input type="text" class="form-control" id="discount" value="{{ present }}">
        </div>
        <button class="btn btn-primary" id="save-btn">保存</button>
    </div>

    <!-- 引入 Bootstrap JS 和 jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // static/js/edit_item.js
$(document).ready(function() {
    $("#save-btn").click(function() {
        var level = $("#level").val();
        var discount = $("#discount").val();

        // 在这里可以进行数据验证，确保输入合法

        // 发送Ajax请求保存数据
        $.ajax({
            type: "POST",
            {#url: "/level/save_item/",#}
            url:"/level/edit_item/",
            data: {
                'id': {{ id }},
                'level': level,
                'discount': discount,
                'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success: function(response) {
                // 在保存成功时显示提示，并返回到主页面
                alert("保存成功!");
                window.location.href = "/level/list/";
            },
            error: function(xhr, errmsg, err) {
                console.log(xhr.status + ": " + xhr.responseText);
                // 这里可以处理错误情况
            }
        });
    });
});

    </script>
</body>
</html>
